package com.atguigu.gulimall.product.web;

import com.atguigu.common.utils.JwtUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.fegin.UserLoginInfo;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.webvo.Catalog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {
    @Autowired
    private RedissonClient redisson;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    HttpServletRequest request;
    @Autowired
    private UserLoginInfo info;

    @GetMapping({"index.html", "/"})
    public String indexPage(String token,Model model) {
        Integer member= JwtUtils.getMemberId(token);

       if(member!=null){
           Long aLong=new Long(member);
           R info = this.info.info(aLong);
           Map userInfo = (Map) info.get("member");
           model.addAttribute("userinfo",userInfo);
          // System.out.println(userInfo);
       }

        //获取所有一级分类
        List<CategoryEntity> catagories = categoryService.getLevel1Catagories();
        model.addAttribute("catagories", catagories);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catalog2Vo>> getCatalogJson() {
        //获取一级分类下所有的2、3级分类
        Map<String, List<Catalog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }

    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        //获取一把锁
        RLock lock = redisson.getLock("my-lock");
        //加锁
        try {
            lock.lock();
            System.out.println("加锁成功...." + Thread.currentThread().getId());
            Thread.sleep(30000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //解锁
            lock.unlock();
            System.out.println("解锁成功" + Thread.currentThread().getId());
        }
        return "hello";
    }

    //读写锁
    @ResponseBody
    @GetMapping("/writeLock")
    public String writeLock() {
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lcok");
        String s = "";
        RLock rLock = lock.writeLock();

        try {
            rLock.lock();
            s = UUID.randomUUID().toString();
            Thread.sleep(30000);
            redisTemplate.opsForValue().set("writeValue", s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return s;
    }

    @ResponseBody
    @GetMapping("/readLock")
    public String readLock() {
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lcok");
        String s = "";
        RLock rLock = lock.readLock();
        rLock.lock();
        try {
            s = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return s;
    }

    //信号量
    @ResponseBody
    @GetMapping("/park")
    public String park() {
        RSemaphore park = redisson.getSemaphore("park");
        try {
            park.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "ok";
    }

    @ResponseBody
    @GetMapping("/go")
    public String go() {
        RSemaphore park = redisson.getSemaphore("park");
        park.release();
        return "ok";
    }

}
